package com.tim.helper.file;

import com.tim.MainHelper;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *@describe 文本格式转换
 *@author Tim
 *@create 2022/9/21 15:03
 */
public class TxtConverter {

    @Test// 转xmind逻辑图，md格式
    public void txt2XmindLogicMd() throws Exception {
        String path = "E:\\github\\Autil\\src\\main\\resources\\maven\\screw\\tableRef.yml";

        String regex = "  ";// 行匹配前缀，替换为#

        List<String> list = Files.readAllLines(Paths.get(path));
        List<String> resultList = new ArrayList<>();
        String prefix = "##";
        Pattern pattern = Pattern.compile("^("+regex+")");
        for(String str : list){
            Matcher matcher = pattern.matcher(str);
            if(matcher.find()){
                String group = matcher.group();
                str = matcher.replaceFirst(StringUtils.repeat('#', group.length()/regex.length()));
            }
            str = (prefix+str).replaceFirst("^(#+)", "$1 ");
            resultList.add(str+"\n");
        }
        File file = new File(MainHelper.OUTPUT_DIR);
        if(!file.exists()){
            file.mkdirs();
        }
        Files.write(Paths.get(MainHelper.OUTPUT_DIR, "xmind"+new Date().getTime()+".md"), resultList);
    }



}
